<?php
namespace app\controllers;
use yii\web\Controller;
use app\models\Orderform;
use app\models\User;
use app\models\Orderform_commodity;
use app\models\Commodity;

require dirname(dirname(__FILE__)).'/excel/PHPExcel.php';//导入excel

class OrderformController extends Controller{

    public function init(){
        $this->enableCsrfValidation = false;
        if(!\Yii::$app->user->id){
            $url = \Yii::$app->request->getPathInfo();
            return $this->redirect('../login/index?url='.$url);
        }
        $userId = \Yii::$app->user->id;
        $user = new User();
        $admin = $user->findIdentity($userId);
        if($admin['type']==1 or $admin['type']==0){
            return $this->redirect('../login/logout');
        }
    }


    public function actionIndex(){
        $view = \Yii::$app->view;
        $view->params['menuitemid']=31;

        return $this->render('index');
    }

    //订单的列表
    public function actionOrderformTableData(){

        //获取相应字段
        $request = \Yii::$app->request;
        $status = $request->get('status');
        $limit = $request->get('limit');
        $offset = $request->get('offset');
        $search = $request->get('search');
        $model = new Orderform();
        $rs = $model->getTableData($status,$limit,$offset,$search);
        echo json_encode($rs);

    }

    //快递公司的列表
    public function actionExpressCompany(){
        $rs = [
            ["shunfeng","顺丰速运"],
            ["yuantong","圆通快递"],
            ["zhongtong","中通快递"],
            ["shentong","申通快递"],
            ["huitongkuaidi","百世快递"],
            ["yunda","韵达快递"],
            ["tiantian","天天快递"],
            ["ems","EMS快递"],
            ["dhl","DHL快递"],
            ["debangwuliu","德邦快递"],
            ["quanfengkuaidi","全峰快递"],
            ["youshuwuliu","优速快递"],
            ["quanyikuaidi","全一快递"],
            ["zhaijisong","宅急送"],
            ["rufengda","如风达"],
        ];
        echo json_encode($rs);
    }

    //发货操作
    public function actionDispatch(){

        //获取相应字段
        $request = \Yii::$app->request;
        $orderform_id = $request->get('orderform_id');
        $express_company = $request->get('express_company');
        $express_num = $request->get('express_num');
        $orderform = Orderform::findOne(['orderform_id' => $orderform_id]);
        $orderform->express_company = $express_company;
        $orderform->express_num = $express_num;
        $orderform->status = 4;//标志发货状态
        $rs=$orderform->save();
        echo json_encode($rs);

    }

    //退款操作
    public function actionRefund(){

        //获取相应字段
        $request = \Yii::$app->request;
        $orderform_id = $request->get('orderform_id');
        $orderform = Orderform::findOne(['orderform_id' => $orderform_id]);
        $orderform->status = 3;//标志发货状态
        $rs=$orderform->save();
        echo json_encode($rs);

    }

    //退货操作
    public function actionReturn(){

        //获取相应字段
        $request = \Yii::$app->request;
        $orderform_id = $request->get('orderform_id');
        $orderform = Orderform::findOne(['orderform_id' => $orderform_id]);
        $orderform->status = 6;//标志发货状态
        $rs=$orderform->save();
        echo json_encode($rs);
    }

    //关闭订单操作
    public function actionClose(){

        //获取相应字段
        $request = \Yii::$app->request;
        $orderform_id = $request->get('orderform_id');
        $orderform = Orderform::findOne(['orderform_id' => $orderform_id]);
        $orderform->status = 8;//标志关闭状态
        $commodityArr= Orderform_commodity::findAll(['orderform_id' => $orderform_id]);
        for($i=0;$i<count($commodityArr);$i++){
            $stock_num = Commodity::find()->where(['commodity_id'=>$commodityArr[$i]])->select('stock_num')->scalar();
            \Yii::$app->db->createCommand()
                ->update('hcps_commodity', ['stock_num'=>$commodityArr[$i]['commodity_num']+$stock_num], 'commodity_id='.$commodityArr[$i])->execute();
        }

        $rs=$orderform->save();
        echo json_encode($rs);

    }

    //查看物流
    public function actionCheckExpress(){

        //获取相应字段
        $request = \Yii::$app->request;
        $orderform_id = $request->get('orderform_id');
        $rs = (new \yii\db\Query())->select('express_company,express_num,remarks')
            ->from('hcps_orderform')
            ->where(['del_flag' => 1,'orderform_id'=>$orderform_id])
            ->one();
        echo json_encode($rs);

    }

    //该订单下商品的列表
    public function actionCommodityTableData(){

        //获取相应字段
        $request = \Yii::$app->request;
        $orderform_id = $request->get('orderform_id');
        $model = new Orderform_commodity();
        $rs = $model->getTableData($orderform_id);
        echo json_encode($rs);

    }

    //导出Excel
    public  function actionExport()
    {
        $objectPHPExcel = new \PHPExcel();

        $model = new Orderform();
        for($j=-1;$j<9;$j++) {
            $data = $model->getAllTableData($j);
            $objectPHPExcel->createSheet();

            $objectPHPExcel->setActiveSheetIndex($j+1);

            //报表头的输出
            $objectPHPExcel->getActiveSheet($j+1)->mergeCells('B1:G1');
            $objectPHPExcel->getActiveSheet($j+1)->setCellValue('B1', '订单核算表');
            $objectPHPExcel->getActiveSheet($j+1)->setCellValue('B2', '日期：' . date("Y年m月j日"));

            //表格头的输出
            $objectPHPExcel->getActiveSheet($j+1)->getColumnDimension('A')->setWidth(5);
            $objectPHPExcel->setActiveSheetIndex($j+1)->setCellValue('B3', '订单号');
            $objectPHPExcel->getActiveSheet($j+1)->getColumnDimension('B')->setWidth(20);
            $objectPHPExcel->setActiveSheetIndex($j+1)->setCellValue('C3', '订单信息');
            $objectPHPExcel->getActiveSheet($j+1)->getColumnDimension('C')->setWidth(40);
            $objectPHPExcel->setActiveSheetIndex($j+1)->setCellValue('D3', '买家账号');
            $objectPHPExcel->getActiveSheet($j+1)->getColumnDimension('D')->setWidth(15);
            $objectPHPExcel->setActiveSheetIndex($j+1)->setCellValue('E3', '收货人姓名');
            $objectPHPExcel->getActiveSheet($j+1)->getColumnDimension('E')->setWidth(15);
            $objectPHPExcel->setActiveSheetIndex($j+1)->setCellValue('F3', '收货人号码');
            $objectPHPExcel->getActiveSheet($j+1)->getColumnDimension('F')->setWidth(20);
            $objectPHPExcel->setActiveSheetIndex($j+1)->setCellValue('G3', '收货人地址');
            $objectPHPExcel->getActiveSheet($j+1)->getColumnDimension('G')->setWidth(40);
            $objectPHPExcel->setActiveSheetIndex($j+1)->setCellValue('H3', '交易状态');
            $objectPHPExcel->getActiveSheet($j+1)->getColumnDimension('H')->setWidth(15);
            $objectPHPExcel->setActiveSheetIndex($j+1)->setCellValue('I3', '实收金额');
            $objectPHPExcel->getActiveSheet($j+1)->getColumnDimension('I')->setWidth(15);
            $objectPHPExcel->setActiveSheetIndex($j+1)->setCellValue('J3', '创建时间');
            $objectPHPExcel->getActiveSheet($j+1)->getColumnDimension('J')->setWidth(20);

            //设置居中
            $objectPHPExcel->getActiveSheet($j+1)->getStyle('B3:J3')
                ->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);

            //设置边框
            $objectPHPExcel->getActiveSheet($j+1)->getStyle('B3:J3')
                ->getBorders()->getTop()->setBorderStyle(\PHPExcel_Style_Border::BORDER_THIN);
            $objectPHPExcel->getActiveSheet($j+1)->getStyle('B3:J3')
                ->getBorders()->getLeft()->setBorderStyle(\PHPExcel_Style_Border::BORDER_THIN);
            $objectPHPExcel->getActiveSheet($j+1)->getStyle('B3:J3')
                ->getBorders()->getRight()->setBorderStyle(\PHPExcel_Style_Border::BORDER_THIN);
            $objectPHPExcel->getActiveSheet($j+1)->getStyle('B3:J3')
                ->getBorders()->getBottom()->setBorderStyle(\PHPExcel_Style_Border::BORDER_THIN);
            $objectPHPExcel->getActiveSheet($j+1)->getStyle('B3:J3')
                ->getBorders()->getVertical()->setBorderStyle(\PHPExcel_Style_Border::BORDER_THIN);

            //设置颜色
            $objectPHPExcel->getActiveSheet()->getStyle('B3:J3')->getFill()
                ->setFillType(\PHPExcel_Style_Fill::FILL_SOLID)->getStartColor()->setARGB('FF66CCCC');

            //遍历得到对应的值
            for ($i = 0; $i < count($data); $i++) {

                $str = $data[$i]['commodity_name'];

                $objectPHPExcel->getActiveSheet($j+1)->setCellValueExplicit('B' . ($i + 4), $data[$i]['orderform_id'], \PHPExcel_Cell_DataType::TYPE_STRING2);
                $objectPHPExcel->getActiveSheet($j+1)->setCellValueExplicit('C' . ($i + 4), $str . '合计' . $data[$i]['commodity_count'] . '类商品', \PHPExcel_Cell_DataType::TYPE_STRING2);
                $objectPHPExcel->getActiveSheet($j+1)->setCellValueExplicit('D' . ($i + 4), $data[$i]['user_name'], \PHPExcel_Cell_DataType::TYPE_STRING2);
                $objectPHPExcel->getActiveSheet($j+1)->setCellValueExplicit('E' . ($i + 4), $data[$i]['addressbook']['fullname'], \PHPExcel_Cell_DataType::TYPE_STRING2);
                $objectPHPExcel->getActiveSheet($j+1)->setCellValueExplicit('F' . ($i + 4), $data[$i]['addressbook']['mobile'], \PHPExcel_Cell_DataType::TYPE_STRING2);
                $objectPHPExcel->getActiveSheet($j+1)->setCellValueExplicit('G' . ($i + 4), $data[$i]['addressbook']['province'] . $data[$i]['addressbook']['city'] . $data[$i]['addressbook']['district'] . $data[$i]['addressbook']['address'], \PHPExcel_Cell_DataType::TYPE_STRING2);
                $objectPHPExcel->getActiveSheet($j+1)->setCellValueExplicit('H' . ($i + 4), $data[$i]['status_name'], \PHPExcel_Cell_DataType::TYPE_STRING2);
                $objectPHPExcel->getActiveSheet($j+1)->setCellValueExplicit('I' . ($i + 4), $data[$i]['money'], \PHPExcel_Cell_DataType::TYPE_STRING2);
                $objectPHPExcel->getActiveSheet($j+1)->setCellValueExplicit('J' . ($i + 4), $data[$i]['create_time'], \PHPExcel_Cell_DataType::TYPE_STRING2);
            }

            ob_end_clean();
            ob_start();
        }
        $objectPHPExcel->setActiveSheetIndex(0)->setTitle('全部订单');
        $objectPHPExcel->setActiveSheetIndex(1)->setTitle('待付款');
        $objectPHPExcel->setActiveSheetIndex(2)->setTitle('待发货');
        $objectPHPExcel->setActiveSheetIndex(3)->setTitle('退款中');
        $objectPHPExcel->setActiveSheetIndex(4)->setTitle('已退款');
        $objectPHPExcel->setActiveSheetIndex(5)->setTitle('待收货');
        $objectPHPExcel->setActiveSheetIndex(6)->setTitle('退货中');
        $objectPHPExcel->setActiveSheetIndex(7)->setTitle('已退货');
        $objectPHPExcel->setActiveSheetIndex(8)->setTitle('已完成');
        $objectPHPExcel->setActiveSheetIndex(9)->setTitle('已关闭');
        $objectPHPExcel->setActiveSheetIndex(0);


        header('Content-Type : application/vnd.ms-excel');
        header('Content-Disposition:attachment;filename="' . '订单信息表-' . date("Y年m月j日") . '.xls"');
        $objWriter = \PHPExcel_IOFactory::createWriter($objectPHPExcel, 'Excel5');
        $objWriter->save('php://output');
    }
}